package joe.beer;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class AddBeerActivity extends Activity {

	RadioButton radio16, radio12;
	AutoCompleteTextView textView;
	Button okButton;
	String delme = "a";
	MySQLiteHelper dbHelper;
	Beer bm;


	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.addbeer);
		dbHelper = new MySQLiteHelper(this);
		textView = (AutoCompleteTextView) findViewById(R.id.autocomplete_country);
		okButton = (Button) findViewById(R.id.button1);
		ArrayAdapter<Beer> adapter = new ArrayAdapter<Beer>(this, R.layout.list_item, dbHelper.getAllBeersList());
		textView.setAdapter(adapter);
		textView.setOnItemClickListener(new OnItemClickListener() { 

			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) { 
				bm = (Beer) arg0.getAdapter().getItem((int) arg3);;
			}
		});

		okButton.setOnClickListener(buttonHandler);
		radio16 = (RadioButton) findViewById(R.id.radio_size16);
		radio12 = (RadioButton) findViewById(R.id.radio_size12);

	}

	// Create an anonymous implementation of OnClickListener
	private OnClickListener buttonHandler = new OnClickListener() {
		public void onClick(View v) {
			finish();
		}
	};

	@Override
	public void finish() {

		int beerSize = 12;
		if (radio16.isChecked())
			beerSize=16;
		
		// we need them to select a valid beer from the list
		if (bm == null || !bm.toString().equals(textView.getText().toString()))
		{
			AlertDialog ad = new AlertDialog.Builder(this).create();
			ad.setCancelable(false); // This blocks the 'BACK' button
			ad.setTitle("Invalid Beer");
			ad.setMessage("Please select a valid beer from the list");
			ad.setButton("OK", new DialogInterface.OnClickListener() {
			    public void onClick(DialogInterface dialog, int which) {
			        dialog.dismiss();                    
			    }
			});
			ad.show();
		}
		else
		{
		dbHelper.AddBeer(bm, beerSize);
		Intent data = new Intent();
		setResult(RESULT_OK, data);
		super.finish();
		}
		
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (dbHelper != null) {
			dbHelper.close();
		}
	}

	//http://www.realsimple.com/holidays-entertaining/entertaining/food-drink/alcohol-content-wine-00000000005947/index.html
	static final String[] WINES = new String[] {
		"Red", "White"
	};
	static final String[] SHOTS = new String[] {
		"Whiskey", "Vokda"
	};
}
